package com.itheima.d2_buffered_stream;

import java.io.*;

/*
    目标：观察原始流和缓冲流的性能。
 */
public class TimeTest4 {
    public static void main(String[] args) throws IOException {
        // 缓存字节流复制文件
        String mp4Path = "D:\\ITCAST\\唐老师\\JavaSE进阶\\day09-字符集、IO流（一）\\视频\\03. 编码和解码.mp4";
        String savePath = "D:\\data\\relay.mp4";

        long start = System.currentTimeMillis();
//        copyBuffer(mp4Path, savePath);
        copyBytes(mp4Path, savePath);
        final long end = System.currentTimeMillis();
        System.out.println("耗时：" + (end - start) + " 毫秒");
    }

    public static void copyBuffer(String sourcePath, String targetPath) throws IOException {
        // 创建缓冲流
        final BufferedInputStream bis = new BufferedInputStream(new FileInputStream(sourcePath));
        final BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(targetPath));

        // 读取写入数据
        int len = -1;
        byte[] bytes = new byte[1024];

        while ((len = bis.read(bytes)) != -1) {
            bos.write(bytes, 0, len);
        }

        // 关闭流
        bis.close();
        bos.close();
    }

    public static void copyBytes(String sourcePath, String targetPath) throws IOException {
        // 创建缓冲流
        final FileInputStream fis = new FileInputStream(sourcePath);
        final FileOutputStream fos = new FileOutputStream(targetPath);

        // 读取写入数据
        int len = -1;
        byte[] bytes = new byte[1024];

        while ((len = fis.read(bytes)) != -1) {
            fos.write(bytes, 0, len);
        }

        // 关闭流
        fis.close();
        fos.close();
    }
}










